package week04.homework01MazeGame.battle;

import week04.homework01MazeGame.model.role.Monster;
import week04.homework01MazeGame.model.role.Player;

import java.util.Scanner;

/**
 * @author li
 */
public class BattleController {

    private static final Scanner SCANNER = new Scanner(System.in);

    public static boolean battle(Player player, Monster monster) {
        System.out.println("你遇到了一个" + monster.getGrowth().getLevelName() + "怪物！");
        monster.showMessage();
        System.out.println("--------------------进入战斗状态---------------------");
        while (player.getHealth() > 0 && monster.getHealth() > 0) {
            if (chooseAttackMethod(player, monster)) {
                if (monster.getHealth() > 0) {
                    monster.attack(player);
                }
            } else {
                return false;
            }
        }
        if (player.getHealth() > 0 && monster.getHealth() <= 0) {
            System.out.println("恭喜你打败了怪物！");
            System.out.println("获得经验值：" + monster.getExp());
            player.addExp(monster.getExp());
            return true;
        }
        System.out.println("---------------------------------------------------");
        return false;
    }

    private static boolean chooseAttackMethod(Player player, Monster monster) {
        System.out.println("请选择攻击方式：");
        System.out.println("1. 普通攻击; 2. 法术攻击; 3. 自动操作; 4. 逃跑; 5. 查看状态");
        String input = SCANNER.nextLine();
        switch (input) {
            case "1" -> player.attack(monster);
            case "2" -> player.magicAttack(monster);
            case "3" -> autoAttack(player, monster);
            case "4" -> {
                System.out.println("你逃跑了！");
                return false;
            }
            case "5" -> {
                player.showMessage();
                monster.showMessage();
            }
            default -> System.out.println("无效的输入，请重新选择！");
        }
        return true;
    }

    private static void autoAttack(Player player, Monster monster) {
        while (player.getHealth() > 0 && monster.getHealth() > 0) {
            player.attack(monster);
            if (monster.getHealth() > 0) {
                monster.attack(player);
            }
        }
    }

}
